
//
//  HYUserInfoManager.m
//  HYNeiHan
//
//  Created by 上官惠阳 on 16/9/25.
//  Copyright © 2016年 上官惠阳. All rights reserved.
//

#import "HYUserInfoManager.h"
#import "HYUserInfoModel.h"
#import "HYFileCacheManager.h"

static HYUserInfoManager *_singleton = nil;
@implementation HYUserInfoManager

+ (instancetype)sharedManager {

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _singleton = [[HYUserInfoManager alloc] init];
    });
    return _singleton;
}

// 当前用户信息
- (HYUserInfoModel *)currentUserInfo {

    id obj = [HYFileCacheManager getObjectByFileName:NSStringFromClass([HYUserInfoModel class])];
    if (obj != nil) {
        return  obj;
    }
    return nil;
}

// 重置用户信息
- (void)resetUserInfoWithUserInfo:(HYUserInfoModel *)userInfo {
    [userInfo archive];
}

// 登陆
- (void)didLoginInWithUserInfo:(id)userInfo {

    HYUserInfoModel *userinfo = [HYUserInfoModel modelWithDictionary:userInfo];
    [userinfo archive];

    [HYFileCacheManager saveUserData:@YES forKey:HYHasLoginFlag];
}

// 退出登陆
- (void)didLoginOut {
    [HYFileCacheManager removeObjectByFileName:NSStringFromClass([HYUserInfoModel class])];

    [HYFileCacheManager saveUserData:@NO forKey:HYHasLoginFlag];
}

// 判断是否是登陆状态
- (BOOL)isLogin {
    return [[NSUserDefaults standardUserDefaults] boolForKey:HYHasLoginFlag];
}
@end
